/**
 * 
 */
package com.music.service.folders.dao.impl;

import static com.music.service.folders.constants.FolderSQLQueries.ADD_ALBUM_TO_FOLDER;
import static com.music.service.folders.constants.FolderSQLQueries.ADD_FOLDER;
import static com.music.service.folders.constants.FolderSQLQueries.CHECK_FOLDER_MEMBER_PERMISSION;
import static com.music.service.folders.constants.FolderSQLQueries.DELETE_ALBUM_FROM_FOLDER;
import static com.music.service.folders.constants.FolderSQLQueries.DELETE_FOLDER;
import static com.music.service.folders.constants.FolderSQLQueries.EDIT_FOLDER_NAME;
import static com.music.service.folders.constants.FolderSQLQueries.VIEW_ACTIVE_FOLDERS;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.music.service.folders.beans.Folder;
import com.music.service.folders.constants.FolderState;
import com.music.service.folders.dao.FolderDAO;
import com.music.service.folders.exceptions.PermissionDeniedException;
import com.music.service.folders.mapper.CountMapper;
import com.music.service.folders.mapper.FolderMapper;

/**
 * @author asanthan
 * 
 */
public class FolderDAOImpl implements FolderDAO {
	private JdbcTemplate mMusicsTemplate;

	

	/**
	 * @return the musicsTemplate
	 */
	public JdbcTemplate getMusicsTemplate() {
		return mMusicsTemplate;
	}

	/**
	 * @param pMusicsTemplate the musicsTemplate to set
	 */
	public void setMusicsTemplate(JdbcTemplate pMusicsTemplate) {
		mMusicsTemplate = pMusicsTemplate;
	}

	public boolean addFolder(final Long pMemberId, final String pFolderName) {
		int nRowsUpdated = getMusicsTemplate().update(ADD_FOLDER,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pMemberId);
						preparedStatement.setString(2, pFolderName);
						preparedStatement.setInt(3, FolderState.ACTIVE
								.getState());
					}
				});
		return nRowsUpdated == 1;
	}

	public boolean deleteFolder(final Long pMemberId, final Long pFolderId) {
		int nRowsUpdated = getMusicsTemplate().update(DELETE_FOLDER,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setInt(1, FolderState.DELETED
								.getState());
						preparedStatement.setLong(2, pMemberId);
						preparedStatement.setLong(3, pFolderId);
						preparedStatement.setInt(4, FolderState.ACTIVE.getState());
					}
				});
		return nRowsUpdated == 1;
	}

	public boolean editFolderName(final Long pMemberId, final Long pFolderId,
			final String pFolderName) {
		int nRowsUpdated = getMusicsTemplate().update(EDIT_FOLDER_NAME,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setString(1, pFolderName);
						preparedStatement.setLong(2, pMemberId);
						preparedStatement.setLong(3, pFolderId);
						preparedStatement.setInt(4, FolderState.ACTIVE.getState());
					}
				});
		return nRowsUpdated == 1;
	}

	@SuppressWarnings("unchecked")
	public List<Folder> viewActiveFolders(final Long pMemberId) {
		List folders = getMusicsTemplate().query(VIEW_ACTIVE_FOLDERS,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pMemberId);
						preparedStatement.setInt(2, FolderState.ACTIVE.getState());
					}
				}, new FolderMapper());
		return folders;

	}

	public boolean addAlbumToFolder(final Long pMemberId, final Long pFolderId,
			final Long pAlbumId) throws PermissionDeniedException {
		if (checkMemberPermissionForFolder(pMemberId, pFolderId)) {
			int nRowsUpdated = getMusicsTemplate().update(ADD_ALBUM_TO_FOLDER,
					new PreparedStatementSetter() {
						public void setValues(
								PreparedStatement preparedStatement)
								throws SQLException {
							preparedStatement.setLong(1, pFolderId);
							preparedStatement.setLong(2, pAlbumId);
							preparedStatement.setInt(3, FolderState.ACTIVE
									.getState());
						}
					});
			return nRowsUpdated == 1;
		} else {
			throw new PermissionDeniedException("You are not owner of this folder");
		}

	}

	public boolean deleteAlbumFromFolder(final Long pMemberId,final Long pFolderId, final Long pAlbumId)
			throws PermissionDeniedException {
		if (checkMemberPermissionForFolder(pMemberId, pFolderId)) {
			int nRowsUpdated = getMusicsTemplate().update(
					DELETE_ALBUM_FROM_FOLDER, new PreparedStatementSetter() {
						public void setValues(
								PreparedStatement preparedStatement)
								throws SQLException {
							preparedStatement.setLong(1, pFolderId);
							preparedStatement.setLong(2, pAlbumId);
						}
					});
			return nRowsUpdated == 1;
		} else {
			throw new PermissionDeniedException("You are not owner of this folder");
		}
	}

	@SuppressWarnings("unchecked")
	public boolean checkMemberPermissionForFolder(final Long pMemberId,
			final Long pFolderId) {
		List<Integer> counts = getMusicsTemplate().query(
				CHECK_FOLDER_MEMBER_PERMISSION, new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pFolderId);
						preparedStatement.setLong(2, pMemberId);
						preparedStatement.setInt(3, FolderState.ACTIVE
								.getState());
					}
				}, new CountMapper());

		if (!counts.isEmpty()) {
			return counts.get(0) == 1;
		}
		return false;
	}

}
